package org.sean.framework.config;

import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.sean.framework.context.SpringApplicationContext;
import org.sean.framework.redis.BooleanRedisTemplate;
import org.sean.framework.redis.DefaultObjectRedisTemplate;
import org.sean.framework.redis.GzipRedisTemplate;
import org.sean.framework.redis.NumberRedisTemplate;
import org.sean.framework.redis.RedisService;
import org.sean.framework.redis.StringRedisTemplate;
import org.sean.framework.web.config.WebConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.scheduling.annotation.EnableScheduling;

/**
 * 自动配置
 */
@EnableScheduling
@Configuration
@Import({WebConfiguration.class, SpringApplicationContext.class})
public class ApplicationConfiguration {

    @Configuration
    @ConditionalOnClass(RedisOperations.class)
    @ConditionalOnProperty(prefix = "spring.redis", name = "database")
    static class RedisConfiguration {

        @Bean
        @Autowired
        public DefaultObjectRedisTemplate objectTemplate(RedisConnectionFactory redisConnectionFactory) {
            return new DefaultObjectRedisTemplate(redisConnectionFactory) {
            };
        }

        @Bean
        @Autowired
        public StringRedisTemplate stringTemplate(RedisConnectionFactory redisConnectionFactory) {
            return new StringRedisTemplate(redisConnectionFactory);
        }

        @Bean
        @Autowired
        public NumberRedisTemplate numberTemplate(RedisConnectionFactory redisConnectionFactory) {
            return new NumberRedisTemplate(redisConnectionFactory);
        }

        @Bean
        @Autowired
        public BooleanRedisTemplate booleanTemplate(RedisConnectionFactory redisConnectionFactory) {
            return new BooleanRedisTemplate(redisConnectionFactory);
        }

        @Bean
        @Autowired
        public GzipRedisTemplate<Object, Object> gzipRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
            return new GzipRedisTemplate<>(redisConnectionFactory);
        }


        @Bean
        @Autowired
        public RedisService redisService(RedisConnectionFactory redisConnectionFactory) {
            return new RedisService(stringTemplate(redisConnectionFactory),
                    numberTemplate(redisConnectionFactory),
                    gzipRedisTemplate(redisConnectionFactory));
        }
    }

    @Configuration
    @ConditionalOnClass(SqlSessionFactory.class)
    @AutoConfigureAfter(DataSourceAutoConfiguration.class)
    static class MybatisConfiguration {

        /**
         * 分页插件
         */
        @Bean
        public PaginationInnerInterceptor paginationInterceptor() {
            PaginationInnerInterceptor paginationInterceptor = new PaginationInnerInterceptor();
            paginationInterceptor.setMaxLimit(1000L);
            return paginationInterceptor;
        }

    }
}
